package org.nch.videoLauncher.view;

import org.nch.videoLauncher.domain.Profile;
import org.nch.videoLauncher.presenter.AddEditProfilePresenter;

import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListDataListener;
import java.util.List;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Dec 13, 2008
 * Time: 9:04:29 PM
 * To change this template use File | Settings | File Templates.
 */
public class AddEditProfileView implements IContentPanel, IAddEditProfileView {
    private JPanel mainPanel;
    private JPanel profileListPanel;
    private JPanel profileDetailPanel;
    private JTextField nameText;
    private JTextField baseVideoPathText;
    private JLabel nameLabel;
    private JLabel baseVideoPathLabel;
    private JList profileList;
    private JPanel buttonPanel;
    private JButton addProfileButton;
    private JButton deleteProfileButton;
    private JTextField openUrlCommandText;
    private JLabel openurlLabel;
    private JTextField vlcCommandText;
    private JLabel vlcCommandLabel;
    DefaultListModel listData;

    private AddEditProfilePresenter presenter;
    private Integer currentListRow;

    private DefaultListModel listModel;

    public AddEditProfileView(){

        currentListRow = -1;

        profileList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent event) {
                if (event.getValueIsAdjusting())
                        return;

                if (listModel.size() == 0)
                        return;

                if (!profileList.isSelectionEmpty()) {

                        presenter.selectionChanged();
                        currentListRow = profileList.getSelectedIndex();
                        System.out.println("current list row: " + currentListRow);
                } else {
                    if (listModel.size() > currentListRow){
                        setCurrentSelection(currentListRow);
                        System.out.println("current list row: " + currentListRow);                         
                    }
               }
            }
        });
        addProfileButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.addProfile();
                nameText.requestFocus();
            }
        });
        deleteProfileButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.deleteProfile();
            }
        });
        nameText.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.detailsChanged();
            }
        });
        baseVideoPathText.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.detailsChanged();
            }
        });

        listModel = new DefaultListModel();
        profileList.setModel(listModel);

        openUrlCommandText.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.detailsChanged();
            }
        });
        vlcCommandText.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.detailsChanged();
            }
        });
    }

    public JPanel getRootComponent() {
        return this.mainPanel;
    }


    public void setPresenter(AddEditProfilePresenter presenter) {
        this.presenter = presenter;
    }

    public void setDetailName(String name) {
        nameText.setText(name);
    }

    public void setDetailBaseVideoPath(String baseVideoPath) {
        baseVideoPathText.setText(baseVideoPath);
    }

    public Integer getCurrentSelection() {
        return profileList.getSelectedIndex();
    }

    public void updateListElement(Integer index, String name) {
        listModel.setElementAt(name, index);
    }

    public void setCurrentSelection(Integer index) {
        profileList.getSelectionModel().setSelectionInterval(index, index);
        currentListRow = index;
    }

    public String getDetailName() {
        return nameText.getText();
    }

    public String getDetailBaseVideoPath() {
        return baseVideoPathText.getText();
    }

    public void setDetailUrlCommand(String command) {
        openUrlCommandText.setText(command);
    }

    public String getDetailUrlCommand() {
        return openUrlCommandText.getText();
    }

    public void setDetailVlcCommand(String command) {
        vlcCommandText.setText(command);
    }

    public String getDetailVlcCommand() {
        return vlcCommandText.getText();
    }

    public void setDetailsEnabled(Boolean enabled) {
        Component[] list = profileDetailPanel.getComponents();
        for (Component c: list){
            c.setEnabled(enabled);
        }
    }

    public void clearDetails() {
        nameText.setText("");
        baseVideoPathText.setText("");
    }

    public void addListElement(String name){
        listModel.addElement(name);
    }

    public void removeListElement(Integer index){
        listModel.remove((int)index);
    }
}
